<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-store, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
    <META HTTP-EQUIV="expires" CONTENT="0">
    <title>新增制程模板</title>
    <link rel="stylesheet" type="text/css" href="./css/main.css" />
    <link rel="stylesheet" type="text/css" href="./layui/css/layui.css" />
    <link rel="stylesheet" type="text/css" href="./css/layuiAdminTheme.css" />
    <link rel="stylesheet" href="layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/public.css" media="all" />

    <style type="text/css">
    </style>
</head>
<body>

<script src="./js/main.js" charset="utf-8"></script>
<!--    <script src="https://cdn.jsdelivr.net/npm/jquery" charset="utf-8"></script>-->
<script src="./layui/layui.js" charset="utf-8"></script>
<script src="./js/jQuery.main.js"></script>
<script src="./js/xm-select.js" type="text/javascript" charset="utf-8"></script>

<form class="layui-form" action="" lay-filter="data">

    <div class="layui-form-item" >
        <label class="layui-form-label" style="width: 7em;">模板名：</label>
        <div class="layui-input-inline " style="width: 16em">
            <input class="layui-input" placeholder="请输入模板名." onkeyup="value=value.replace(/[^\u4e00-\u9fa5A-Za-z0-9\_\-\&]/g,'')" name="mouldName" id="mouldName" lay-verify="required" lay-filter="mouldName">
            <span>可使用的符号:-_&</span>
        </div>
    </div>

    <div class="layui-form-item" >
        <label class="layui-form-label" style="width: 7em;">选择部门：</label>
        <div class="layui-input-inline " style="width: 15em">
            <select name="deptName" id="deptName" lay-verify="required" lay-filter="deptName">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 7em;">选择产品：</label>
        <div class="layui-input-inline " style="width: 18em">
            <!--            <input type="text" name="pinName" lay-verify="required" autocomplete="" class="layui-input" style="width: 18em;"-->
            <!--                       placeholder="请输入品名/PN">-->
            <select name="pinName" id="pinName" lay-verify="required" lay-filter="pinName" lay-search="">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 7em;">CORE品名：</label>
        <div class="layui-input-inline " style="width: 18em">
            <!--            <input type="text" name="pinName" lay-verify="required" autocomplete="" class="layui-input" style="width: 18em;"-->
            <!--                       placeholder="请输入品名/PN">-->
            <div id="coreIds" lay-filter="coreIds" style="width: 45em;"></div>
        </div>
    </div>


    <div class="layui-form-item" style="margin: 10px">
        <label class="layui-form-label"></label>
        <button type="button" class="layui-btn" lay-submit=''>添加模板</button>
        <span>( Tips:新增模板信息后,请回到管理页上传模板文件,否则无法使用! )</span>
    </div>
</form>


<script type="text/javascript" src="./layui/layui.js"></script>
<script type="text/javascript">
    var form = null;
    let isAd=null;
    let thisDept=null;
    var list = [];
    //所需工序
    var allProcess=[]
    var upload = null;
    var choose_file_flag=false;




    layui.use(['form',"layer",'upload'], function() {
        form = layui.form;
        upload=layui.upload;
        $.ajax({
            url: url + '/sys-dept/listDept',
            method: 'POST',
            async : false,
            contentType: 'application/json',
            success: function(res) {
                //显示前先判断是否为管理员
                $.ajax({
                    url: '/sys-user/getAdmin/'+sessionStorage.getItem('userId'),
                    method: 'GET',
                    async : false,
                    contentType: 'application/json',
                    success: function (res) {
                        if (res.code===200){
                            isAd=1;
                        }
                        else if (res.code===400){
                            isAd=0;
                            thisDept=res.data.dept;
                        }
                    }
                })
                //按照是否为管理员显示下拉框
                if (res.code == 200) {
                    for (var i = 0; i < res.data.length; i++) {
                        if(isAd===1){
                            $("#deptName").append("<option value='" + res.data[i].id + "'>" + res.data[i].dept +"</option>");
                        }else if (isAd===0&&res.data[i].dept===thisDept){
                            $("#deptName").append("<option selected='selected' value='" + res.data[i].id + "'>" + res.data[i].dept +"</option>").attr('disabled','disabled');
                        }
                    }
                    form.render('select');
                }
            }
        })

        //用户下拉框默认化
        $.ajax({
            url: url + '/sys-user/userList',
            method: 'POST',
            async : false,
            contentType: 'application/json',
            data: JSON.stringify({
                userId:"",
                deptId: $('#deptName').val()
            }),
            success: function(res) {
                if (res.code == 200) {
                    for (var i = 0; i < res.data.length; i++) {
                        $("#nowWorker").append("<option value='" + res.data[i].loginName + "'>" + res.data[i].userName +
                            "</option>");
                    }
                    form.render('select');
                }
            }
        })

        //品名下拉框默认
        $.ajax({
            url: url + '/pin-library/listPinName',
            method: 'POST',
            async : false,
            contentType: 'application/json',
            data: JSON.stringify({
                userId:"",
                deptId: $('#deptName').val()
            }),
            success: function(res) {
                if (res.code == 200) {
                    for (var i = 0; i < res.data.length; i++) {
                        $("#pinName").append("<option value='" + res.data[i].id + "'>" + res.data[i].pinName +"</option>");
                    }
                }
                //产品类型下拉框默认
                $.ajax({
                    url: url + '/pin-library/listProductType/'+$('#pinName').val(),
                    method: 'GET',
                    async : false,
                    contentType: 'application/json',
                    success: function(res) {
                        $("#productType").attr('disabled','disabled').empty();
                        if (res.code == 200) {
                            $("#productType").append("<option value='" + res.data.productType + "' selected='selected'>" + res.data.productType + "</option>");
                        }
                        allProcess=res.data.process.split(",")
                    }
                })
                form.render('select');
            }
        })

        //core品名下拉框
        var coreOptions = {
            el: '#coreIds',
            name: 'coreIds',//表单的name属性
            id: 'myCoreIds',
            layVerify: 'required',//必填项
            filterable: true,
            //remoteSearch: true, //自定义搜索
            //layVerType: 'tips',//提示类型 同layui
            tips: '请选择CORE...',
            toolbar: {//工具条,全选,清空,反选,自定义
                show: true,
                list: [
                    'ALL',
                    'CLEAR',
                    'REVERSE',
                    {
                        name: '(★请按顺序选取)',
                        icon: 'el-icon-star-off',
                    }
                ]
            },
            data: [],
        };

        var core = xmSelect.render(coreOptions)

        setTimeout(function(){
            $.ajax({
                url: url + '/core-library/getCoreName',
                method: 'POST',
                async : false,
                data: JSON.stringify({
                    deptId: $('#deptName').val()
                }),
                contentType: 'application/json',
                success: function(res) {
                    //data是ajax 异步获取的
                    var data =[];
                    for (var i = 0; i < res.data.length; i++) {
                        data.push({
                            name : res.data[i].corePin,
                            value :res.data[i].id
                        });
                    }
                    //模拟通过ajax 获取json数据,异步更新多选下拉框的值
                    core.update({data: data})
                }
            })
        },100);


        form.on('submit', function() {
            $.ajax({
                url: '/sys-user/getAdminLevel/'+sessionStorage.getItem('userId'),
                method: 'GET',
                contentType: 'application/json',
                success: function (res) {
                    if (res.code === 200) {
                        var data = form.val('data');
                        data.deptId=$('#deptName').val()
                        data.pinId=$('#pinName').val()
                        let confirm1=layer.confirm('确定新增模板吗？', {
                            btn: ['确定','取消']
                        },function () {
                            $.ajax({
                                url: url + '/process-condition/addMould',
                                method: 'POST',
                                contentType: 'application/json',
                                data: JSON.stringify(data),
                                success: function (res) {
                                    parent.layer.alert(res.msg);
                                    if (res.code == 200) parent.switchNav(0);
                                }
                            })
                            layer.close(confirm1);
                        },function (){
                            layer.close(confirm1);
                        })
                    }else {
                        layer.msg("权限不足");
                    }
                }
            })
        })

        //下拉框二级联动
        form.on('select(deptName)', function(data) {
            //品名下拉框联动
            $.ajax({
                url: url + '/pin-library/listPinName',
                method: 'POST',
                async : false,
                contentType: 'application/json',
                data: JSON.stringify({
                    userId:"",
                    deptId: $('#deptName').val()
                }),
                success: function(res) {
                    $("#pinName").empty();
                    if (res.code == 200) {
                        for (var i = 0; i < res.data.length; i++) {
                            $("#pinName").append("<option value='" + res.data[i].id + "'>" + res.data[i].pinName +
                                "</option>");
                        }
                        form=layui.form;
                        form.render('select');
                    }
                }
            })
            //core品名下拉框
            var coreOptions = {
                el: '#coreIds',
                name: 'coreIds',//表单的name属性
                id: 'myCoreIds',
                layVerify: 'required',//必填项
                filterable: true,
                //remoteSearch: true, //自定义搜索
                //layVerType: 'tips',//提示类型 同layui
                tips: '请选择CORE...',
                toolbar: {//工具条,全选,清空,反选,自定义
                    show: true,
                    list: [
                        'ALL',
                        'CLEAR',
                        'REVERSE',
                        {
                            name: '(★请按顺序选取)',
                            icon: 'el-icon-star-off',
                        }
                    ]
                },
                data: [],
            };

            var core = xmSelect.render(coreOptions)

            setTimeout(function(){
                $.ajax({
                    url: url + '/core-library/getCoreName',
                    method: 'POST',
                    async : false,
                    data: JSON.stringify({
                        deptId: $('#deptName').val()
                    }),
                    contentType: 'application/json',
                    success: function(res) {
                        //data是ajax 异步获取的
                        var data =[];
                        for (var i = 0; i < res.data.length; i++) {
                            data.push({
                                name : res.data[i].corePin,
                                value :res.data[i].id
                            });
                        }
                        //模拟通过ajax 获取json数据,异步更新多选下拉框的值
                        core.update({data: data})
                    }
                })
            },100);
            //产品类型下拉框联动
            $.ajax({
                url: url + '/pin-library/listProductType/'+$('#pinName').val(),
                method: 'GET',
                async : false,
                contentType: 'application/json',
                success: function(res) {
                    $("#productType").attr('disabled','disabled').empty();
                    if (res.code == 200) {
                        $("#productType").append("<option value='" + res.data.productType + "' selected='selected'>" + res.data.productType + "</option>");
                    }
                    allProcess=res.data.process.split(",")
                }
            })
            form=layui.form;
            form.render('select');
        });

        //下拉框二级联动
        form.on('select(pinName)', function(data) {
            //品名下拉框联动产品类型
            $.ajax({
                url: url + '/pin-library/listProductType/'+$('#pinName').val(),
                method: 'GET',
                async : false,
                contentType: 'application/json',
                success: function(res) {
                    $("#productType").attr('disabled','disabled').empty();
                    if (res.code == 200) {
                        $("#productType").append("<option value='" + res.data.productType + "' selected='selected'>" + res.data.productType + "</option>");
                    }
                    allProcess=res.data.process.split(",")
                }
            })
            form=layui.form;
            form.render('select');
        });

    });



</script>
</body>

</html>
